﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;

namespace AutoUpdater {
    /// <summary>
    /// 压缩类
    /// </summary>
    public class ZipClass {
        //设置进度条的委托
        public delegate void SetProgressDelegate(int maximun, string msg);
        #region 解压
        /// <summary>
        /// 解压Zip格式的文件
        /// </summary>
        /// <param name="zipFilePath">压缩文件路径，全部路径格式</param>
        /// <param name="unZipDir">解压文件存放路径，全部路径，为空时默认与压缩文件同一级目录下，跟压缩文件同名的文件夹</param>
        /// <param name="maximum"></param>
        /// <param name="setProgressDelegate"></param>
        /// <returns></returns>
        public static bool UnZip(string zipFilePath, string unZipDir, int maximum, SetProgressDelegate setProgressDelegate) {
            if (zipFilePath == string.Empty) {
                throw new System.IO.FileNotFoundException("压缩文件不能为空！");
            }
            if (!File.Exists(zipFilePath)) {
                throw new FileNotFoundException("压缩文件：" + zipFilePath + "不存在！");
            }
            //解压文件夹为空时默认与压缩文件同一级目录下，跟压缩文件同名的文件夹
            if (unZipDir == string.Empty) {
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), "");
            }
            if (!unZipDir.EndsWith("//")) {
                unZipDir += "//";
            }
            if (!Directory.Exists(unZipDir)) {
                Directory.CreateDirectory(unZipDir);
            }
            try {
                using (ZipInputStream zipInput = new ZipInputStream(File.OpenRead(zipFilePath))) {
                    ZipEntry zipEntry;
                    while ((zipEntry = zipInput.GetNextEntry()) != null) {
                        string directoryName = Path.GetDirectoryName(zipEntry.Name);
                        string fileName = Path.GetFileName(zipEntry.Name);
                        if (directoryName.Length > 0) {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("//")) {
                            directoryName += "//";
                        }
                        if (fileName != string.Empty) {
                            using (FileStream fileStream = File.Create(unZipDir + zipEntry.Name)) {
                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true) {
                                    size = zipInput.Read(data, 0, data.Length);
                                    if (size > 0) {
                                        fileStream.Write(data, 0, size);
                                    } else {
                                        setProgressDelegate(maximum, zipEntry.Name);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                throw new Exception(ex.Message);
            }
            return true;
        }
        #endregion

        /// <summary>
        /// 获取最大值
        /// </summary>
        /// <param name="zips">压缩文件数组</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static int GetMaximum(string[] zips, string filePath) {
            int maximum = 0;
            ZipInputStream zipInput = null;
            ZipEntry zipEntry = null;
            foreach (string zip in zips) {
                zipInput = new ZipInputStream(File.OpenRead(filePath + @"\" + zip));
                while ((zipEntry = zipInput.GetNextEntry()) != null) {
                    if (Path.GetFileName(zipEntry.Name) != "") {
                        maximum++;
                    }
                }
            }
            if (zipInput != null) {
                zipInput.Close();
            }
            return maximum;
        }
    }
}
